﻿using SmartHotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace SmartHotel.DAL
{
    public class OrderDAL
    {

        public OrderDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }

        public int AddOrder(OrderModel order)
        {
            var snowid = YitIdHelper.NextId(); //雪花id
            order.OrderCode = snowid.ToString();

            HotelContext.OrderModels.Add(order);
            return HotelContext.SaveChanges();
        }
        public List<OrderModel> GetOrders(int index, int size, out int count, string? username)
        {

            var ordders = HotelContext.OrderModels.AsQueryable();

            ordders = ordders.Where(x => x.State != 0);
            if (!string.IsNullOrEmpty(username))
            {
                ordders = ordders.Where(x => x.UserName == username);
            }
            count = ordders.Count();
            return ordders.OrderBy(x => x.Id).Skip((index - 1) * size)
                .Take(size)
                .ToList();

        }
        public OrderModel GetOrderId(int id)
        {
            return HotelContext.OrderModels.Where(x => x.Id == id).FirstOrDefault();
        }

        public int LjDel(int id)
        {
            var order = GetOrderId(id);
            if (order != null)
            {
                order.State = 0;
            }
            return HotelContext.SaveChanges();
        }
        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="id">订单id</param>
        /// <returns>返回是否成功,大于0成功</returns>
        public int ZhiFu(int id)
        {
            int result = 0;
            using (var trans = HotelContext.Database.BeginTransaction())//上下文.数据库.开始事务
            {
                try
                {   //支付时,修改订单状态
                    var order = GetOrderId(id);
                    if (order != null)
                    {
                        order.orderStatusEnum = OrderStatusEnum.已支付;
                    }
                    else
                    { throw new Exception("订单为空"); }
                    //检查用户是否存在
                    var name = order.UserName;//前端存到cookie里的用户名
                    var member = HotelContext.Members.Where(x => x.Name == name).FirstOrDefault();
                    if (member != null) { }
                    else { throw new Exception("用户为空"); }
                    //修改,给用户加积分
                    member.JifenVal += Convert.ToInt32(order.TotalPrice) * 10;//消费的金额*10
                    //添加积分记录表
                    JiFen jiFen = new JiFen();
                    jiFen.Time = DateTime.Now;
                    jiFen.JiFenCount = Convert.ToInt32(order.TotalPrice) * 10;
                    jiFen.Reason = "定客房消费了" + order.TotalPrice + "元送积分";
                    jiFen.UserId = member.Id;
                    HotelContext.JiFens.Add(jiFen);

                    HotelContext.SaveChanges();//同步到数据库里

                    trans.Commit(); //提交事务
                    result = 1;
                }
                catch (Exception e)
                {
                    trans.Rollback();//回滚事务
                    //result = 0;
                }
            }
            return result;
        }
    }
}
